package com.xyz.service;

import com.free.exception.SystemException;
import com.free.ext.shiro.SessionManager;
import com.free.utils.DateUtils;
import com.free.utils.JsonUtils;
import com.xyz.domain.constants.EhCacheConstants;
import com.xyz.domain.table.CCustomer;
import com.xyz.utils.ThreadLocalUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import java.util.Date;
import java.util.Random;

public class BaseService {

    @Autowired
    protected EhCacheCacheManager ehCacheCacheManager;

    protected static final String[] NUMS = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0"};

    protected CCustomer getCurrentUser(){
        return (CCustomer) ThreadLocalUtils.get(ThreadLocalUtils.ACCOUNT);
    }

    protected String randomNo(Date current){
        return DateUtils.convert2String(current, DateUtils.FormatEnum.yyyyMMddHHmmss) + createData(4);
    }

    protected String recommendNo(){
        return DateUtils.convert2String(new Date(),"MMdd") + randomNumbers(4);
    }

    public boolean isExistsInQueue(String cacheCode){
        Object cacheVal = ehCacheCacheManager.getCache(EhCacheConstants.OneTimeQueue).get(cacheCode);
        return cacheVal != null;
    }

    public void putToQueue(String cacheCode, Object val){
        ehCacheCacheManager.getCache(EhCacheConstants.OneTimeQueue).put(cacheCode,val);
    }

    public void deleteFromQueue(String cacheCode){
        ehCacheCacheManager.getCache(EhCacheConstants.OneTimeQueue).evict(cacheCode);
    }

    public void putStringToQueue(String cacheName, String key, String val){
        ehCacheCacheManager.getCache(cacheName).put(key, val);
    }

    public String getStringFromQueue(String cacheName, String key){
        return ehCacheCacheManager.getCache(cacheName).get(key, String.class);
    }

    public <T> void putObjToCache(String cahceName,String key, T obj) throws SystemException {
        String afterSeriable = JsonUtils.searlize(obj);
        putStringToQueue(cahceName, key, afterSeriable);
    }

    public <T> T getObjectFromCache(String cacheName, String key, Class<T> clazz) throws SystemException {
       String res = getStringFromQueue(cacheName,key);
        if(StringUtils.isEmpty(res)){
            return null;
        }else{
           return JsonUtils.unsearlize(res,clazz);
        }
    }

    public void cahceEvict(String cacheName, String key){
        ehCacheCacheManager.getCache(cacheName).evict(key);
    }

    public String randomNumbers(int length){
        StringBuffer sbuf = new StringBuffer();
        Random rand=new Random();
        for(int i=0;i<length;i++)
        {
            int num = rand.nextInt(36);
            sbuf.append(NUMS[num]);
        }
        return sbuf.toString();
    }

    public String createData(int length) {
        StringBuilder sb=new StringBuilder();
        Random rand=new Random();
        for(int i=0;i<length;i++)
        {
            sb.append(rand.nextInt(10));
        }
        String data=sb.toString();
        return data;
    }

    public static void main(String[] args) {
        Random rand=new Random();
        for(int i=0;i<1000;i++){
            int num = rand.nextInt(36);
            System.out.println(NUMS[num]);

        }
    }


}
